package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.OaSecondaryUnit;
import com.ruoyi.system.domain.vo.UnitVo;
import com.ruoyi.system.domain.vo.UnitVoSs;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OaUnit;
import com.ruoyi.system.service.IOaUnitService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单位Controller
 *
 * @author ruoyi
 * @date 2025-04-01
 */
@RestController
@RequestMapping("/system/unit")
public class OaUnitController extends BaseController {
    @Autowired
    private IOaUnitService oaUnitService;

    /**
     * 查询单位列表
     */
    @PreAuthorize("@ss.hasPermi('system:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(OaUnit oaUnit) {
        startPage();
        List<OaUnit> list = oaUnitService.selectOaUnitList(oaUnit);
        return getDataTable(list);
    }

    /**
     * 导出单位列表
     */
    @PreAuthorize("@ss.hasPermi('system:unit:export')")
    @Log(title = "单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OaUnit oaUnit) {
        List<OaUnit> list = oaUnitService.selectOaUnitList(oaUnit);
        ExcelUtil<OaUnit> util = new ExcelUtil<OaUnit>(OaUnit.class);
        util.exportExcel(response, list, "单位数据");
    }

    /**
     * 获取单位详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:unit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(oaUnitService.selectOaUnitById(id));
    }

    /**
     * 新增单位
     */
    @PreAuthorize("@ss.hasPermi('system:unit:add')")
    @Log(title = "单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OaUnit oaUnit) {
        return toAjax(oaUnitService.insertOaUnit(oaUnit));
    }

    /**
     * 修改单位
     */
    @PreAuthorize("@ss.hasPermi('system:unit:edit')")
    @Log(title = "单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OaUnit oaUnit) {
        return toAjax(oaUnitService.updateOaUnit(oaUnit));
    }

    /**
     * 删除单位
     */
    @PreAuthorize("@ss.hasPermi('system:unit:remove')")
    @Log(title = "单位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(oaUnitService.deleteOaUnitByIds(ids));
    }

    @GetMapping(value = "/selectUnitList")
    public AjaxResult selectUnitList(String oaUnit) {
        List<UnitVo> unitInfoByUnitName = oaUnitService.getUnitInfoByUnitName(oaUnit);
        Set<String> industries = new HashSet<>();
        Set<String> secondaryUnitNames = new HashSet<>();

        for (UnitVo unitVo : unitInfoByUnitName) {
            industries.add(unitVo.getIndustry());
            secondaryUnitNames.add(unitVo.getSecondaryUnitName());
        }

        AtomicInteger unitVoSsIdGenerator = new AtomicInteger(1);
        List<UnitVoSs> unitVoSses = new ArrayList<>();
        for (String industry : industries) {
            UnitVoSs unitVoSs = new UnitVoSs();
            unitVoSs.setSecondaryUnitName(industry);
            unitVoSs.setId( unitVoSsIdGenerator.getAndIncrement());
            unitVoSses.add(unitVoSs);
        }

        AtomicInteger oaSecondaryUnitIdGenerator = new AtomicInteger(1);
        List<OaSecondaryUnit> oaSecondaryUnits = new ArrayList<>();
        for (String secondaryUnitName : secondaryUnitNames) {
            OaSecondaryUnit oaSecondaryUnit = new OaSecondaryUnit();
            oaSecondaryUnit.setSecondaryUnitName(secondaryUnitName);
            oaSecondaryUnit.setId((long) oaSecondaryUnitIdGenerator.getAndIncrement());
            oaSecondaryUnits.add(oaSecondaryUnit);
        }

        for (UnitVoSs ss : unitVoSses) {
            ss.setOaSecondaryUnit(oaSecondaryUnits);
        }

        return AjaxResult.success(unitVoSses);
    }
}
